import configs from '@/common/config.js'

// 微信公众号授权
export function wxAuthorize(callback) {
	// 非静默授权，第一次有弹框
	let local = window.location.href; // 获取页面url
	
	let appid = configs.wxappid // 公众号appid
	let code = getUrlCode().code; // 截取code
	// 获取之前的code
	let oldCode = uni.getStorageSync('wechatCode')
	if (code == null || code === '' || code == 'undefined' || code == oldCode) {
		// 如果没有code，就去请求获取code
		
		let uri = encodeURIComponent(local)
		// 设置旧的code为0，避免死循环
		uni.setStorageSync('wechatCode', 0)
		window.location.href =
			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
	} else {
		uni.setStorageSync('wechatCode', code)
		uni.request({
			method: 'POST',
			url: configs.baseUrl + '/api/glogin', // 你的接口地址
			data: {
				code,
				invitecode: uni.getStorageSync('invitecode') ? uni.getStorageSync('invitecode') : ''
			},
			success: res => {
				if (res.data.code == 1) {
					callback(res.data)
					
				} else {
					uni.$u.toast(res.data.msg)
					// 失败的逻辑
					
				}
			},
			fail: (err) => {
				window.alert('请求失败')
				
			}
		});
	}
}

// vuex存储
// function vuex(name, value) {
// 	store.commit('$uStore', {
// 		name,
// 		value
// 	})
// }


function getUrlCode() {
	// 截取url中的code方法
	var url = location.search;
	// this.winUrl = url;
	var theRequest = new Object();
	if (url.indexOf('?') != -1) {
		var str = url.substr(1);
		var strs = str.split('&');
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
		}
	}
	return theRequest;
}
